package com.hutou.chuangke.activity;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bilibili.magicasakura.widgets.TintImageView;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.hutou.chuangke.Bean.HouseBean;
import com.hutou.chuangke.Bean.UserBean;
import com.hutou.chuangke.MainApplication;
import com.hutou.chuangke.R;
import com.hutou.chuangke.fragment.MoreFragment;
import com.hutou.chuangke.fragmentnet.CollectionsFragment;
import com.hutou.chuangke.handler.HandlerUtil;
import com.hutou.chuangke.info.MusicInfo;
import com.hutou.chuangke.net.HttpUtil;
import com.hutou.chuangke.net.RequestThreadPool;
import com.hutou.chuangke.net.http.User;
import com.hutou.chuangke.service.MusicPlayer;
import com.hutou.chuangke.uitl.BroadcastString;
import com.hutou.chuangke.uitl.CommonUtils;
import com.hutou.chuangke.uitl.FileUtil;
import com.hutou.chuangke.uitl.IConstants;
import com.hutou.chuangke.uitl.PictureCutUtil;
import com.hutou.chuangke.widget.CircleImageView;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by o on 2017/4/15.
 */
public class UserInfoActivity extends BaseActivity implements View.OnClickListener {
    Toolbar toolbar;
    private UserBean user;
    private CircleImageView head_iv;
    private TextView edite_tv;
    private TextView username_tv,username_motto;
    private ImageView sex_iv;
    private TextView follow_tv,fans_tv,my_music_tv,music_more_text,my_collection_tv,collection_more_text,isfollow_tv;
    private RecyclerView music_recyclerviewew,collection_recyclerviewew;
    private LinearLayout mymusic_layout,mycollection_layout,fans_layout;
    private static final int PHOTO_REQUEST_CAMERA = 1;
    private static final int PHOTO_REQUEST_GALLERY = 2;
    private static final int PHOTO_REQUEST_CUT = 3;
    private static final int UPDATE_USER_INFO = 4;
    private Bitmap bitmap;
    private ArrayList<String> imageurls = new ArrayList<>();
    ArrayList<HouseBean> houselist = new ArrayList<>();
    ArrayList<MusicInfo> mList = new ArrayList<>();
    UserCollectionAdapter mAdapter;
    private Context mContext;
    private PictureCutUtil pictureCutUtil;
    private RequestUploadhead requestUploadhead;
    private Requestgetuserinfo requestgetuserinfo;
    private LoadCollectionListInfo loadCollectionListInfo;
    private Dialog mCameraDialog ;
    private File tempFile;
    private String userid;
    private static final String PHOTO_FILE_NAME = "image.jpg";
    private static final String CORE_FILE_PATH = Environment.getExternalStorageDirectory() + File.separator+"ledong";
    private boolean ismyself = false;//是否是自己


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_myuser_info);
        initView();
        initData();
    }



    private void initView() {
        mContext = this;
        pictureCutUtil = new PictureCutUtil(mContext);
        Intent intent = getIntent();
        if(intent!=null){
            userid = intent.getStringExtra("userid");
            ismyself = intent.getBooleanExtra("ismyself",false);
        }

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setPadding(0, CommonUtils.getStatusHeight(UserInfoActivity.this), 0, 0);
        setSupportActionBar(toolbar);
        final ActionBar ab = getSupportActionBar();
        ab.setHomeAsUpIndicator(R.drawable.actionbar_back);
        ab.setDisplayHomeAsUpEnabled(true);
        ab.setTitle("");
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        showQuickControl(false);

        sex_iv = (ImageView) findViewById(R.id.sex_iv);
        follow_tv = (TextView) findViewById(R.id.follow_tv);
        isfollow_tv = (TextView) findViewById(R.id.isfollow_tv);
        fans_tv = (TextView) findViewById(R.id.fans_tv);
        my_music_tv = (TextView) findViewById(R.id.my_music_tv);
        music_more_text = (TextView) findViewById(R.id.music_more_text);
        my_collection_tv = (TextView) findViewById(R.id.my_collection_tv);
        collection_more_text = (TextView) findViewById(R.id.collection_more_text);
        mymusic_layout = (LinearLayout) findViewById(R.id.mymusic_layout);
        mycollection_layout = (LinearLayout) findViewById(R.id.mycollection_layout);

        music_recyclerviewew = (RecyclerView) findViewById(R.id.music_recyclerviewew);
        music_recyclerviewew.setLayoutManager(new LinearLayoutManager(mContext));
        music_recyclerviewew.setHasFixedSize(true);
//        mAdapter = new UserCollectionAdapter(UserInfoActivity.this, mList);
//        music_recyclerviewew.setAdapter(mAdapter);

        collection_recyclerviewew = (RecyclerView) findViewById(R.id.collection_recyclerviewew);
        collection_recyclerviewew.setLayoutManager(new LinearLayoutManager(mContext));
        collection_recyclerviewew.setHasFixedSize(true);
        mAdapter = new UserCollectionAdapter(UserInfoActivity.this, mList);
        collection_recyclerviewew.setAdapter(mAdapter);


        head_iv = (CircleImageView) findViewById(R.id.head_iv);
        username_tv = (TextView) findViewById(R.id.username_tv);
        username_motto = (TextView) findViewById(R.id.username_motto);

        head_iv.setOnClickListener(this);
        edite_tv = (TextView) findViewById(R.id.edite_tv);
        edite_tv.setOnClickListener(this);

        fans_layout = (LinearLayout) findViewById(R.id.fans_layout);
        fans_layout.setOnClickListener(this);

        if(ismyself){
            edite_tv.setVisibility(View.VISIBLE);
            isfollow_tv.setVisibility(View.GONE);
        }else{
            edite_tv.setVisibility(View.GONE);
            isfollow_tv.setVisibility(View.VISIBLE);
        }
    }

    private void initData() {
        requestgetuserinfo = new Requestgetuserinfo();
        requestgetuserinfo.execute(userid);

        loadCollectionListInfo = new LoadCollectionListInfo();
        loadCollectionListInfo.execute();
    }


    private View.OnClickListener btnlistener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.btn_open_camera:
                    if (Build.VERSION.SDK_INT>22){
                        if (ContextCompat.checkSelfPermission(UserInfoActivity.this,
                                android.Manifest.permission.CAMERA)!= PackageManager.PERMISSION_GRANTED){
                            //先判断有没有权限 ，没有就在这里进行权限的申请
                            ActivityCompat.requestPermissions(UserInfoActivity.this,
                                    new String[]{android.Manifest.permission.CAMERA},101);

                        }else {
                            //说明已经获取到摄像头权限了 想干嘛干嘛
                            useCamera();
                        }
                    }else {
//这个说明系统版本在6.0之下，不需要动态获取权限。
                        useCamera();

                    }
                    mCameraDialog.dismiss();

                    break;
                case R.id.btn_choose_img:
                    mCameraDialog.dismiss();
                    Intent intent = new Intent(Intent.ACTION_PICK);
                    intent.setType("image/*");
                    startActivityForResult(intent, PHOTO_REQUEST_GALLERY);
                    break;
                case R.id.btn_cancel:
                    mCameraDialog.dismiss();
                    break;
            }
        }
    };

    @Override
    public void onRequestPermissionsResult(int requestCode,  String[] permissions,  int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode){
            case 101:
                if (grantResults.length>0&&grantResults[0]==PackageManager.PERMISSION_GRANTED){
                    //这里已经获取到了摄像头的权限，想干嘛干嘛了可以
                    useCamera();
                }else {
                    //这里是拒绝给APP摄像头权限，给个提示什么的说明一下都可以。
                    Toast.makeText(UserInfoActivity.this,"请手动打开相机权限",Toast.LENGTH_SHORT).show();
                }
                break;
            default:
                break;
        }
    }

    private void useCamera(){
        Intent cameraintent = new Intent("android.media.action.IMAGE_CAPTURE");
        cameraintent.putExtra(MediaStore.EXTRA_OUTPUT,
                Uri.fromFile(new File(Environment
                        .getExternalStorageDirectory(), PHOTO_FILE_NAME)));
        startActivityForResult(cameraintent, PHOTO_REQUEST_CAMERA);
    }

    private void showDialog(){
        if(mCameraDialog == null) {
            mCameraDialog = new Dialog(this, R.style.camera_photo_dialog);
            LinearLayout root = (LinearLayout) LayoutInflater.from(this).inflate(
                    R.layout.dialog_layout_camera_control, null);
            root.findViewById(R.id.btn_open_camera).setOnClickListener(btnlistener);
            root.findViewById(R.id.btn_choose_img).setOnClickListener(btnlistener);
            root.findViewById(R.id.btn_cancel).setOnClickListener(btnlistener);
            mCameraDialog.setContentView(root);
            Window dialogWindow = mCameraDialog.getWindow();
            dialogWindow.setGravity(Gravity.BOTTOM);
        dialogWindow.setWindowAnimations(R.style.dialogstyle); // 添加动画
            WindowManager.LayoutParams lp = dialogWindow.getAttributes(); // 获取对话框当前的参数值
            lp.x = 0; // 新位置X坐标
            lp.y = -20; // 新位置Y坐标
            lp.width = (int) getResources().getDisplayMetrics().widthPixels; // 宽度
//      lp.height = WindowManager.LayoutParams.WRAP_CONTENT; // 高度
//      lp.alpha = 9f; // 透明度
            root.measure(0, 0);
            lp.height = root.getMeasuredHeight();
            lp.alpha = 9f; // 透明度
            dialogWindow.setAttributes(lp);
            mCameraDialog.show();
        }else{
            if(!mCameraDialog.isShowing()){
                mCameraDialog.show();
            }
        }

    }

    @Override
    public void onClick(View v) {
        if(v == head_iv){
            if(ismyself) {
                showDialog();
            }
        }else if(v == edite_tv){
            Intent intent = new Intent(mContext , EditUserInfoActivity.class);
            intent.putExtra("motto",user.getMotto());
            intent.putExtra("singer",user.getSinger());
            intent.putExtra("sex",user.getSex());
            startActivityForResult(intent,UPDATE_USER_INFO);
        }else if(v == fans_layout){
            Intent intent = new Intent(mContext , FansListActivity.class);
            intent.putExtra("ismyself",true);
            startActivity(intent);
        }
    }

    class RequestUploadhead extends AsyncTask<Void , Void ,Boolean> {

        @Override
        protected Boolean doInBackground(Void... voids) {
            try{
                JsonObject jsonObject = HttpUtil.getResposeJsonObject(User.uploadhead(MainApplication.id,MainApplication.sid),imageurls,"photo");
                String flag = jsonObject.get("flag").getAsString();
                if(flag.equals("1")){
                    return true;
                }else{
                    return  false;
                }
            }catch (Exception e){
                e.printStackTrace();
            }
            return false;
        }

        @Override
        protected void onPostExecute(Boolean aBoolean) {
            super.onPostExecute(aBoolean);
            if(aBoolean){
                Toast.makeText(mContext,"上传成功",Toast.LENGTH_SHORT).show();
                File file = new File(CORE_FILE_PATH);
                FileUtil.deleteFile(file);
//                UserInfoActivity.this.finish();
            }else{
                Toast.makeText(mContext,"上传失败",Toast.LENGTH_SHORT).show();
            }
        }

        public void cancleTask(){
            cancel(true);
            RequestThreadPool.finish();
        }
    }

    class Requestgetuserinfo extends AsyncTask<String , Void ,Boolean> {

        @Override
        protected Boolean doInBackground(String... params) {
            try{
                JsonObject jsonObject = HttpUtil.getResposeJsonObject(User.getuserinfo(params[0],MainApplication.sid,MainApplication.id));

                String flag = jsonObject.get("flag").getAsString();
                user = MainApplication.gsonInstance().fromJson(jsonObject.getAsJsonObject("data"),UserBean.class);
                if(flag.equals("1")){
                    return true;
                }else{
                    return  false;
                }
            }catch (Exception e){
                e.printStackTrace();
            }
            return false;
        }

        @Override
        protected void onPostExecute(Boolean aBoolean) {
            super.onPostExecute(aBoolean);
            if(aBoolean){
                Toast.makeText(mContext,"获取成功",Toast.LENGTH_SHORT).show();
                username_tv.setText(user.getSinger());
                username_motto.setText("签名:"+user.getMotto());
                ImageLoader.getInstance().displayImage(user.getUserimgurl(),head_iv);
//                UserInfoActivity.this.finish();
            }else{
                Toast.makeText(mContext,"获取失败",Toast.LENGTH_SHORT).show();
            }
            refreshUi();
        }

        public void cancleTask(){
            cancel(true);
            RequestThreadPool.finish();
        }
    }

    private void refreshUi() {
        if(user!=null){
            username_tv.setText(user.getSinger());
            username_motto.setText("签名:"+user.getMotto());
            ImageLoader.getInstance().displayImage(user.getUserimgurl(),head_iv);
            if(user.getSex().equals("f")){
                sex_iv.setImageDrawable(getResources().getDrawable(R.drawable.me_female));
            }else{
                sex_iv.setImageDrawable(getResources().getDrawable(R.drawable.me_male));
            }
            follow_tv.setText(user.getAttentionnum());
            fans_tv.setText(user.getFansnum());
            try{
                if(Integer.valueOf(user.getAlbum())>0){
                    my_music_tv.setText("我的作品("+user.getAlbum()+")");
                }else{
//                    mymusic_layout.setVisibility(View.GONE);
                }
            }catch (Exception e){
                e.printStackTrace();
            }
            if(user.getIsfollow().equals("y")){
                isfollow_tv.setText("已关注");
                isfollow_tv.setCompoundDrawablesWithIntrinsicBounds(null,null,null,null);
            }else{
                isfollow_tv.setText("+关注");
                isfollow_tv.setCompoundDrawablesWithIntrinsicBounds(getResources().getDrawable(R.drawable.add),null,null,null);
            }
//            my_music_tv.setText("我的作品("+user.getAlbum()+")");
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(requestgetuserinfo != null){
            requestgetuserinfo.cancleTask();
        }
        if(requestUploadhead != null){
            requestUploadhead.cancleTask();
        }
        if(ismyself){
            Intent intent = new Intent();
            intent.setAction(BroadcastString.UPDATE_USER_INFO);
            mContext.sendBroadcast(intent);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (requestCode == PHOTO_REQUEST_GALLERY) {//相册
            if (data != null) {
                // 得到图片的全路径
                Uri uri = data.getData();
                crop(uri);//剪切图片
            }

        } else if (requestCode == PHOTO_REQUEST_CAMERA) {//相机
//            if (hasSdcard()) {
                tempFile = new File(Environment.getExternalStorageDirectory(),
                        PHOTO_FILE_NAME);
                crop(Uri.fromFile(tempFile));//剪切图片
//            } else {
//                showToast("未找到存储卡，无法存储照片！");
//            }

        } else if (requestCode == PHOTO_REQUEST_CUT) {//图片剪裁完成
            try {
                imageurls.clear();
                bitmap = data.getParcelableExtra("data");
                head_iv.setImageBitmap(bitmap);
                String filePath = pictureCutUtil.cutPictureQuality(bitmap, "ledong");
                imageurls.add(filePath);
                requestUploadhead = new RequestUploadhead();
                requestUploadhead.execute();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }else if(requestCode == UPDATE_USER_INFO){
            requestgetuserinfo = new Requestgetuserinfo();
            requestgetuserinfo.execute(MainApplication.id);
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    /**
     * 剪切图片
     *
     */
    private void crop(Uri uri) {
        // 裁剪图片意图
        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(uri, "image/*");
        intent.putExtra("crop", "true");
        // 裁剪框的比例，1：1
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        // 裁剪后输出图片的尺寸大小
        intent.putExtra("outputX", 250);
        intent.putExtra("outputY", 250);
        // 图片格式
        intent.putExtra("outputFormat", "PNG");
        intent.putExtra("noFaceDetection", true);// 取消人脸识别
        intent.putExtra("return-data", true);// true:不返回uri，false：返回uri
        startActivityForResult(intent, PHOTO_REQUEST_CUT);//同样的在onActivityResult中处理剪裁好的图片
    }


    class LoadCollectionListInfo extends AsyncTask<Void,Void,Boolean>{

        @Override
        protected Boolean doInBackground(Void... voids) {
            try{
                JsonObject list = HttpUtil.getResposeJsonObject(User.getCollection(MainApplication.id,MainApplication.sid,1,"3",1,6),mContext,false);
                JsonArray houseList = list.get("houseList").getAsJsonArray();//mv

                String msg = list.get("msg").getAsString();
                for(JsonElement element : houseList){
                    houselist.add(MainApplication.gsonInstance().fromJson(element,HouseBean.class));
                }
                for(HouseBean houseBean : houselist){
                    MusicInfo musicInfo = new MusicInfo();
//                    musicInfo.songId = 1;
//                    musicInfo.musicName = soundBean.getSoundname();
//                    musicInfo.artist = soundBean.getSinger();
//                    musicInfo.islocal = false;
//                    musicInfo.albumName = "未知";
//                    musicInfo.albumId = -1;
//                    musicInfo.artistId = Long.valueOf(soundBean.getUserid());
//                    musicInfo.lrc = soundBean.getLyric();
//                    musicInfo.albumData = soundBean.getSoundurlimg();

                    musicInfo.songId = 1;
                    musicInfo.musicName = houseBean.getSoundname();
                    musicInfo.artist = houseBean.getSinger();
                    musicInfo.islocal = false;
                    musicInfo.albumName = "未知";
                    musicInfo.albumId = 1;
                    musicInfo.artistId = Long.valueOf(houseBean.getUserid());
                    musicInfo.lrc = houseBean.getLyric();
//                    musicInfo.albumData = houseBean.getSoundurlimg();
                    mList.add(musicInfo);
                }
            }catch(Exception e){
                e.printStackTrace();
            }
            return true;
        }

        @Override
        protected void onPostExecute(Boolean comlete) {

            if (!comlete) {

            } else {
                mAdapter.setdata(mList);
                mAdapter.notifyDataSetChanged();
            }

        }

        public void cancleTask(){
            cancel(true);
            RequestThreadPool.finish();
        }
    }

    class UserCollectionAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
        final static int FIRST_ITEM = 0;
        final static int ITEM = 1;
        private ArrayList<MusicInfo> arraylist = new ArrayList<>();
        private Activity mContext;
        public UserCollectionAdapter(Activity context, ArrayList<MusicInfo> mList) {
            this.arraylist = mList;
            this.mContext = context;
        }

        private  void setdata(ArrayList<MusicInfo> mList){
            this.arraylist = mList;
            notifyDataSetChanged();
        }
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//            if (viewType == FIRST_ITEM) {
//                return new CommonItemViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.common_item, parent, false));
//            } else {
                return new ItemViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_playlist_detail_item, parent, false));
//            }
        }

//        @Override
//        public int getItemViewType(int position) {
//            return position == FIRST_ITEM ? FIRST_ITEM : ITEM;
//        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
            if (holder instanceof ItemViewHolder) {
                final MusicInfo localItem = arraylist.get(position );
                //判断该条目音乐是否在播放
                if (MusicPlayer.getCurrentAudioId() == localItem.songId) {
                    ((ItemViewHolder) holder).trackNumber.setVisibility(View.GONE);
                    ((ItemViewHolder) holder).playState.setVisibility(View.VISIBLE);
                    ((ItemViewHolder) holder).playState.setImageResource(R.drawable.song_play_icon);
                    ((ItemViewHolder) holder).playState.setImageTintList(R.color.theme_color_primary);
                } else {
                    ((ItemViewHolder) holder).playState.setVisibility(View.GONE);
                    ((ItemViewHolder) holder).trackNumber.setVisibility(View.VISIBLE);
                    ((ItemViewHolder) holder).trackNumber.setText(position + "");
                }
                ((ItemViewHolder) holder).title.setText(localItem.musicName);
                ((ItemViewHolder) holder).artist.setText(localItem.artist);
                ((ItemViewHolder) holder).menu.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

//                        if(localItem.islocal){
                        MoreFragment morefragment = MoreFragment.newInstance(arraylist.get(position ).songId + "",
                                IConstants.MUSICOVERFLOW);
                        morefragment.show(((AppCompatActivity) mContext).getSupportFragmentManager(), "music");
//                            return;
//                        }
//                        new AlertDialog.Builder(mContext).setTitle("要下载音乐吗").
//                                setPositiveButton(mContext.getString(R.string.sure), new DialogInterface.OnClickListener() {
//
//                                    @Override
//                                    public void onClick(DialogInterface dialog, int which) {
//
//                                        Down.downMusic(MainApplication.context, localItem.songId + "", localItem.musicName);
//                                        dialog.dismiss();
//                                    }
//                                }).
//                                setNegativeButton(mContext.getString(R.string.cancel), new DialogInterface.OnClickListener() {
//                                    @Override
//                                    public void onClick(DialogInterface dialog, int which) {
//                                        dialog.dismiss();
//                                    }
//                                }).show();
                    }
                });

            } else if (holder instanceof CommonItemViewHolder) {

                ((CommonItemViewHolder) holder).textView.setText("(共" + arraylist.size() + "首)");

                ((CommonItemViewHolder) holder).select.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                    }
                });

            }
        }

        public void updateDataSet(ArrayList<MusicInfo> arraylist) {
            this.arraylist = arraylist;
            this.notifyDataSetChanged();
        }

        @Override
        public int getItemCount() {
            return arraylist == null ? 0 : arraylist.size();
//            return arraylist == null ? 0 : arraylist.size() + 1;
        }

        private class CommonItemViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
            TextView textView;
            ImageView select;
            RelativeLayout layout;

            CommonItemViewHolder(View view) {
                super(view);
                this.textView = (TextView) view.findViewById(R.id.play_all_number);
                this.select = (ImageView) view.findViewById(R.id.select);
                this.layout = (RelativeLayout) view.findViewById(R.id.play_all_layout);
//                layout.setOnClickListener(this);
            }

            public void onClick(View v) {
                //// TODO: 2016/1/20
                HandlerUtil.getInstance(mContext).postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        HashMap<Long, MusicInfo> infos = new HashMap<Long, MusicInfo>();
                        int len = arraylist.size();
                        long[] list = new long[len];
                        for (int i = 0; i < len; i++) {
                            MusicInfo info = arraylist.get(i);
                            list[i] = info.songId;
                            infos.put(list[i], info);
                        }
                        MusicPlayer.playAll(infos, list, 0, false);
                    }
                },70);
            }

        }

        private class ItemViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
            protected TextView title, artist, trackNumber;
            protected ImageView menu;
            TintImageView playState;

            public ItemViewHolder(View view) {
                super(view);
                this.title = (TextView) view.findViewById(R.id.song_title);
                this.artist = (TextView) view.findViewById(R.id.song_artist);
                this.trackNumber = (TextView) view.findViewById(R.id.trackNumber);
                this.menu = (ImageView) view.findViewById(R.id.popup_menu);
                this.playState = (TintImageView) view.findViewById(R.id.play_state);
//                view.setOnClickListener(this);
            }

            @Override
            public void onClick(View v) {
                HandlerUtil.getInstance(mContext).postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        HashMap<Long, MusicInfo> infos = new HashMap<Long, MusicInfo>();
                        int len = arraylist.size();
                        long[] list = new long[len];
                        for (int i = 0; i < len; i++) {
                            MusicInfo info = arraylist.get(i);
                            list[i] = info.songId;
                            infos.put(list[i], info);
                        }
                        if (getAdapterPosition() > 0)
                            MusicPlayer.playAll(infos, list, getAdapterPosition() - 1, false);
                    }
                }, 70);
            }

        }
    }

}
